.toString
在不等待复合的情况下呼唤未来会导致不确定的结果.我的问题是为什么在scala 2.10.x和2.11.x中调用.toString
未完成的期货收益"List()"
?该实现似乎并未明确.
可以从REPL中观察到此行为:
scala> import scala.concurrent.Future, scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global scala> Future(1).toString res0: scala.concurrent.Future[Int] = Success(1) scala> Future(1).toString res1: scala.concurrent.Future[Int] = List()
请注意,Scala 2.12.x可能会显式实现Future#toString
返回"Future(
(源代码).
编辑:证明这不是来自REPL或"隐藏隐藏"的人工制品(-Yno-predef
删除所有默认隐含):
Future.scala:
import scala.concurrent.Future import scala.concurrent.ExecutionContext.Implicits.global object Main extends App { System.out.println(Future(1).toString) }
build.sbt:
scalaVersion := "2.11.8" scalacOptions := Seq( "-deprecation", "-encoding", "UTF-8", "-feature", "-unchecked", "-Yno-predef", "-Xfatal-warnings", "-Xlint", "-Yinline-warnings", "-Yno-adapted-args", "-Ywarn-dead-code", "-Ywarn-unused-import", "-Ywarn-numeric-widen", "-Ywarn-value-discard", "-Xfuture")
Viktor Klang.. 8
消除依赖性是一个不幸的副作用sun.misc.Unsafe
.它在Scala 2.12和更新版本的Scala 2.11 IIRC中得到了纠正.
消除依赖性是一个不幸的副作用sun.misc.Unsafe
.它在Scala 2.12和更新版本的Scala 2.11 IIRC中得到了纠正.